import 'package:flutter/material.dart';
import 'package:x3_base_core/x3_base_core.dart';
import 'package:x3_component_core/x3_component_core.dart';
import 'package:x3_form_core/business_components/base_request_form_container.dart';
import 'package:x3_form_core/provider/base_request_form_provider.dart';

class BaseRequestFormMultipleSelectDialog extends StatefulWidget {
  final BaseHttpRequestModel request;

  final RequestSaveData requestSaveData;

  const BaseRequestFormMultipleSelectDialog({Key key, this.request, this.requestSaveData}) : super(key: key);
  @override
  _BaseRequestFormMultipleSelectDialogState createState() => _BaseRequestFormMultipleSelectDialogState();
}

class _BaseRequestFormMultipleSelectDialogState extends State<BaseRequestFormMultipleSelectDialog> {
  /// 入参
  BaseHttpRequestModel get _request => widget.request;
  BaseRequestFormProvider _baseRequestFormProvider;
  BaseBottomSheetMultipleController _baseBottomSheetController;

  @override
  void initState() {
    _baseBottomSheetController = BaseBottomSheetMultipleController(items: []);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    _baseRequestFormProvider = BaseRequestFormProvider.of(context);

    /// 获取对应值
    _baseBottomSheetController.selected = _baseRequestFormProvider.getDataSource(_request);
    _baseBottomSheetController.setOldData(_baseBottomSheetController.selected);
    return BaseRequestFormContainer(
      suffixIcon: BaseDropDownIcon(),
      showSuffixIcon: !_baseRequestFormProvider.isReadonly(widget.request),
      request: widget.request,
      onPress: _baseRequestFormProvider.isReadonly(_request) == true ? null : _requestItems,
      child: BaseBottomSheetMultipleSelector(
        isIcon: false,
        controller: _baseBottomSheetController,
        disable: true,
        onComplete: (value) {
          _baseRequestFormProvider.setDataSource(_request, value.map((e) => e.toJson()).toList());
          if (widget.requestSaveData != null) {
            widget.requestSaveData(widget.request, _baseRequestFormProvider.getMapDataSource(widget.request));
          }
        },
      ),
    );
  }

  /// 获取item信息
  void _requestItems() async {
    if (_baseBottomSheetController.items.length > 0) {
      _baseBottomSheetController.showBottomSheet();
      return;
    }
    if (_baseBottomSheetController.items.length == 0) {
      if (_request.inputApi != null && _request.inputApi.isNotEmpty) {
        BaseComponentUtils.showLoadingDialog(context);
        BaseHttpResultModel result = await dioUtil.fetch(_request.inputApi, params: {}, method: BaseHttpMethodEnum.GET);
        BaseComponentUtils.hideLoadingDialog(context);
        if (result.success == true) {
          var list = result.data;
          if (list is List) {
            _baseBottomSheetController.items = list.map((e) => BaseBottomSheetSelectorModel(key: e['key'], value: e['value'])).toList();
            _baseBottomSheetController.showBottomSheet();
          }
        }
      }
    }
  }
}
